/**
 
 @Name: layuiSimpleBlog - 极简博客模板
 @Author: xuzhiwen
 @Copyright: layui.com
 
 */
 
 
layui.define(['mm','jquery'],function(exports){
  var $ = layui.$,mm = layui.mm;
  function GetUrlRelativePath()
　　{
　　　　var url = document.location.toString();
　　　　var arrUrl = url.split("//");

　　　　var start = arrUrl[1].indexOf("/");
　　　　var relUrl = arrUrl[1].substring(start);//stop省略，截取从start开始到结尾的所有字符

　　　　if(relUrl.indexOf("?") != -1){
　　　　　　relUrl = relUrl.split("?")[0];
　　　　}
　　　　return relUrl;
　　}

  var menu = {
    init: function(){
      $('.menu').on('click',function(){
        if($(this).hasClass('on')){
          $(this).removeClass('on');
          $('.header-down-nav').removeClass('layui-show');
        }else{
          $(this).addClass('on');
          $('.header-down-nav').addClass('layui-show');
        }
      });
      window.onresize = function () {
        var curwidth = document.documentElement.clientWidth;
        if(curwidth > 760){
          $('.header-down-nav').removeClass('layui-show');
          $('.menu').removeClass('on');
        }
      };
    $(document).on('click','.op-list .like',function(){
      var oSpan =  $(this).children('span');
      var num = parseInt($(oSpan).text());
      var off = $(this).attr('off');
      var whisper_id=$(this).data('id').split('praisePoints_')[1];
        if(off){
          $(this).removeClass('active');
          off = true;
          $(oSpan).text(num-1);
          $(this).attr('off','')
        }else{
          $(this).addClass('active');
          off = false;
          state=true;
          $(oSpan).text(num+1);
          $(this).attr('off','true')
        }
        //更新数字
        $.ajax({
          url:'/praisePoints/whisper',
          data:{'id':whisper_id,'state':off},
          type:'post',
          dataType:'json',
          success:function (res) {
              if(res.status==0){
                $(oSpan).text(' '+res.num);
              }
              else if(res.state==1){
                  if(off){
                    $(this).removeClass('active');
                    off = true;
                    $(oSpan).html(' '+num-1);
                    $(this).attr('off','')
                  }else{
                    $(this).addClass('active');
                    off = false;
                    $(oSpan).html(' '+num+1);
                    $(this).attr('off','true')
                  }
              }
          }
        })
      })
    },
    off:function(){
      $('.off').on('click',function(){
        var off = $(this).attr('off');
        var chi = $(this).children('i');
        var text = $(this).children('span');
        var cont = $(this).parents('.item').siblings('.review-version');
        if(off){
          $(text).text('展开');
          $(chi).attr('class','layui-icon layui-icon-down');
          $(this).attr('off','');
          $(cont).addClass('layui-hide');
        }else{
          $(text).text('收起');
          $(chi).attr('class','layui-icon layui-icon-up')
          $(this).attr('off','true');
          $(cont).removeClass('layui-hide')
        }
      })
    },
    submit: function(email,nickname,btn,edit_index,url,after_bakcall){
        var $listcont = $(btn).parents('.form').siblings('.list-cont').length ? $(btn).parents('.form').siblings('.list-cont') : $(btn).parents('.form-box').siblings('.list-cont');
        var img = $(btn).parents('form').siblings('img').attr('src');
        var textarea = $(btn).parents('.layui-form-item').siblings('.layui-form-text').children('.layui-input-block').children('textarea');
        var content = layedit.getContent(edit_index);
        var html = laytplCont.innerHTML;
        var formdata = {
          email : email,
          nickname:nickname,
          content :content
        };
         var data = {
          avatar : img,
          name : nickname,
          cont : content,
        };
        if(content){
          var param={
            method:'post',
            url:url,
            data:formdata,
            success:function (res,msg) {
              var cont = mm.renderHtml(html,data);
                $listcont.prepend(cont);
                var cunt = $(btn).parents('.form-box').siblings('.volume').children('span');
                var cunts = $(btn).parents('.form-box').siblings('.list-cont').children('.cont').length;
                cunt.text(cunts);

                layer.alert(msg,{icon: 1})
            },
            error:function (msg) {
              layer.alert(msg,{icon: 5})
            }
          };
          mm.request(param);
          after_bakcall();
        }else{
          layer.msg('请输入内容')
        }
    }
  };
  exports('menu',menu)
});